//
//  GoodsEvaluateVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/8.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "GoodsEvaluateVC.h"
#import "EvaluateBottomView.h"
#import "EvaluateStarCell.h"
#import "EvaluateMiddleCell.h"
#import "EvaluateTopCell.h"
#import "EvaluateImagesCell.h"

@interface GoodsEvaluateVC ()<EvaluateBottomViewDelegate>
@property (nonatomic, weak) EvaluateBottomView *bottomView;

@end

@implementation GoodsEvaluateVC




- (EvaluateBottomView *)bottomView{
    
    if (!_bottomView) {
        
        EvaluateBottomView *temp = (EvaluateBottomView *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([EvaluateBottomView class]) owner:nil options:nil].firstObject;
        
        temp.model = self.parmar;
        
        temp.delegate = self;
        
        CGFloat bottomViewH = IPhoneX ? 83 : 49;
        
        temp.frame = CGRectMake(0, KScreenH - bottomViewH, KScreenW, bottomViewH);
        
        _bottomView = temp;
//        
        [self.view addSubview:temp];
        
    }
    
    return _bottomView;
}



- (void)evaluateBtnDidClick{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"uid"] = self.parmar.uid;
    parameters[@"order_id"] = self.parmar.order_id;
    parameters[@"order_no"] = self.parmar.order_no;
    parameters[@"order_goods_id"] = self.parmar.order_goods_id;
    
    if (self.parmar.content == nil) {
        
        [self showInformation:@"请填写评价内容..."];
        
        return;
    }
    
    
    if ([self.parmar.scores isEqualToString:@"0"]) {
        
        [self showInformation:@"请填选择描述相符!"];
        
        return;
    }
    
    
    parameters[@"content"] = self.parmar.content;
    parameters[@"scores"] = self.parmar.scores;
    parameters[@"explain_type"] = self.parmar.explain_type;
    parameters[@"is_anonymous"] = self.parmar.is_anonymous;
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
    //    manger.responseSerializer  = [AFHTTPResponseSerializer serializer];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/plain", @"text/javascript",@"text/html" ,nil];
    
    NSString *url = @"http://feidu.baonongkang.cn/index.php/api/Good/addGoodsEvaluate";
    
    [SVProgressHUD showWithStatus:@"图片上传中请稍等..."];
    

    

    
    MJWeakSelf
    [manger POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        if (weakSelf.parmar.imags.count > 0) {
            
            int i = 0;
            
            for (UIImage *img in weakSelf.parmar.imags) {
                
                NSData * data = UIImageJPEGRepresentation([img getScaledImage], 1.0);
                NSString *name = [NSString stringWithFormat:@"file%d",i];
                [formData appendPartWithFileData:data
                                            name:name
                                        fileName:[NSString stringWithFormat:@"%@.png",name]
                                        mimeType:@"application/octet-stream"];
                
                i++;
                
            }
            
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [SVProgressHUD showSuccessWithStatus:@"评价成功!"];
      
        
        MYJSONSTR;
        [[NSNotificationCenter defaultCenter]postNotificationName:@"DidSusseceEvauate" object:nil];
    
            
            
        [weakSelf performSelector:@selector(back) withObject:nil afterDelay:2.0];
            
      
        
     
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [SVProgressHUD showErrorWithStatus:error.domain];
        
    }];
    
    
    
    
    
    
    
    
}



- (void)showInformation:(NSString *)str{
    
    UIAlertController *contr = [UIAlertController alertControllerWithTitle:nil message:str preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    
//    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//
//    }];
    
    
//    [contr addAction:action2];
    [contr addAction:action1];
    
    [self presentViewController:contr animated:YES completion:nil];
    
    
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    
        self.plainTableView.rowHeight = UITableViewAutomaticDimension;
    
//     self.plainTableView.separatorStyle =
    
        self.plainTableView.estimatedRowHeight = 100;
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EvaluateStarCell class]) bundle:nil] forCellReuseIdentifier:[EvaluateStarCell identifierStr]];
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EvaluateMiddleCell class]) bundle:nil] forCellReuseIdentifier:[EvaluateMiddleCell identifierStr]];

    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EvaluateTopCell class]) bundle:nil] forCellReuseIdentifier:[EvaluateTopCell identifierStr]];
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EvaluateImagesCell class]) bundle:nil] forCellReuseIdentifier:[EvaluateImagesCell identifierStr]];

    self.bottomView.hidden = NO;

    // Do any additional setup after loading the view.
}


- (void)setNav{
    
    self.navigationItem.title = @"评价商品";
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateHighlighted];
    
    [but addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    
    but.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    //        but.backgroundColor = [UIColor redColor];
    
    but.frame = CGRectMake(0, 0, 44, 44);
    but.contentEdgeInsets = UIEdgeInsetsMake(11, -10, 11, 22);
    
    UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
    
    
    self.navigationItem.leftBarButtonItem = returnButtonItem;
}


- (void)back{
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 3;
    } else {
        return 1;
    }
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            EvaluateTopCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[EvaluateTopCell identifierStr]];
            
            tableCell.model = self.parmar;
            return tableCell;
            
        } else if (indexPath.row == 1){
            
            EvaluateMiddleCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[EvaluateMiddleCell identifierStr]];
            tableCell.model = self.parmar;

            tableCell.tableView = self.plainTableView;
            return tableCell;
            
        }else{
            
            EvaluateImagesCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[EvaluateImagesCell identifierStr]];
            
            tableCell.model = self.parmar;

            return tableCell;
            
            
        }
        
        
        
        
    } else {
        
        
        EvaluateStarCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[EvaluateStarCell identifierStr]];
        
        tableCell.model = self.parmar;

        return tableCell;
    }
    
    
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.00001;
    } else {
        return 44;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return [[UIView alloc]init];
    } else {
        UIView *temp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 44)];
        
        temp.backgroundColor = viewBgGrey;
        
        UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, KScreenW, 44)];
        
        leftLabel.text = @"给商品评分";
        
        leftLabel.font = MYFONT(16);
        
        leftLabel.backgroundColor = [UIColor clearColor];
        
        [temp addSubview:leftLabel];
        
        return temp;
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.view endEditing:YES];

    
}




- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.view endEditing:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
